package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_followers;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_followers;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_followersSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_followersService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[文档关注者] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_followersServiceImpl implements IMail_followersService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_followersClientService mail_followersClientService;

    @Override
    public Mail_followers get(Integer id) {
        Imail_followers clientModel = mail_followersClientService.createModel();
        clientModel.setId(id);
        mail_followersClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_followers get(Mail_followers mail_followers) {
        Mail_followers retmail_followers = this.get(mail_followers.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_followers.getClass(), mail_followers.getClass(), false);
        copier.copy(retmail_followers, mail_followers, null);
        return mail_followers ;
    }

    @Override
    public boolean create(Mail_followers mail_followers) {
        boolean bOk = false ;
        Imail_followers clientModel = this.convert2Model(mail_followers,null);
        mail_followersClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_followers);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_followersClientService.removeBatch(mail_followerss) ;

    }

    @Override
    public void updateBatch(List<Mail_followers> mail_followerss, int batchSize) {

        //mail_followersClientService.updateBatch(mail_followerss,batchSize) ;

    }

    @Override
    public boolean update(Mail_followers mail_followers) {
        boolean bOk = false ;
        Imail_followers clientModel = this.convert2Model(mail_followers,null);
        mail_followersClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_followers) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mail_followers> mail_followerss, int batchSize) {

                        //mail_followersClientService.createBatch(mail_followerss,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_followers clientModel = mail_followersClientService.createModel();
        clientModel.setId(id);
        mail_followersClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_followers> searchDefault(Mail_followersSearchContext context) {
        List<Mail_followers> list = new ArrayList<Mail_followers>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_followers> clientModelList = mail_followersClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_followers clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_followers>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据讨论频道查询
     */
    @Override
    public Page<Mail_followers> selectByOdoo_channel(Integer id){
        Mail_followersSearchContext context = new Mail_followersSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_followers> list = new ArrayList<Mail_followers>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_followers> clientModelList = mail_followersClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_followers clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_followers>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Mail_followers> selectByOdoo_partner(Integer id){
        Mail_followersSearchContext context = new Mail_followersSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_followers> list = new ArrayList<Mail_followers>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_followers> clientModelList = mail_followersClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_followers clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_followers>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_followers convert2Model(Mail_followers domain , Imail_followers model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_followersClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getSubtype_idsDirtyFlag())
            model.setSubtype_ids(domain.getSubtype_ids());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getChannel_id_textDirtyFlag())
            model.setChannel_id_text(domain.getChannel_id_text());
         if(domain.getChannel_idDirtyFlag())
            model.setChannel_id(domain.getChannel_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
        return model ;
    }

    public Mail_followers convert2Domain( Imail_followers model ,Mail_followers domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_followers();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getSubtype_idsDirtyFlag())
            domain.setSubtype_ids(model.getSubtype_ids());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getChannel_id_textDirtyFlag())
            domain.setChannel_id_text(model.getChannel_id_text());
        if(model.getChannel_idDirtyFlag())
            domain.setChannel_id(model.getChannel_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        return domain ;
    }


}
